/**
 * Created by 55456 on 2016/4/11.
 */
var should = require("should");
var request = require("superagent");
var db = require("../models/index");
var utilx = require("../lib/util");
var host = "http://127.0.0.1:3000";
var cache = require("../instances/cache");
var User = db.models.User;
var Story = db.models.Story;
var Comment = db.models.Comment;
var Msg = db.models.Msg;

describe("test userinfo and chat", function () {
    var LoginToken;
    var token;
    var userID;
    var storyID;
    var commentID;

    before(function (done) {
        request
            .get(host + "/api/debugLogin")
            .end(function (err, res) {
                token = res.text;
                request
                    .post(host + "/api/getToken")
                    .send({token: token})
                    .end(function (err, res) {
                        var data = JSON.parse(res.text);
                        LoginToken = data.msg.LoginToken;
                        userID = data.msg.userID;
                        done();
                    });
            });
    });

    it("post my id should get total userinfo", function (done) {
        request
            .post(host + "/api/token/userinfo")
            .set("LoginToken", LoginToken)
            .send({AuthorID: userID})
            .end(function (err, res) {
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(err);
                }
                data.status.should.be.exactly(true);
                data.msg.collectedStoriesCount.should.be.exactly(100);
                data.msg.following.length.should.be.exactly(9);
                done();
            });
    });

    it("post other people id should get one userinfo", function (done) {
        request
            .post(host + "/api/token/userinfo")
            .set("LoginToken", LoginToken)
            .send({AuthorID: 3})
            .end(function (err, res) {
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(err);
                }
                data.status.should.be.exactly(true);
                data.msg.following[0].should.be.exactly("false");
                data.msg.follower[0].should.be.exactly("false");
                done()
            });
    });

    it("updateUserInfo should work ok", function (done) {
        request
            .post(host + "/api/token/userinfo/update")
            .set("LoginToken",LoginToken)
            .send({
                avatar: "www.baidu.com",
                userName: "changed",
                sign: "test",
                sex: 1,
                updateNotice:1,
                noticeEnable:1,
                followingEnable:1,
                followerEnable:1,
                aboutNotice:1
            })
            .end(function(err,res){
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(err);
                }
                data.msg.user.avatar.should.be.exactly("www.baidu.com");
                data.msg.user.userName.should.be.exactly("changed");
                data.msg.user.sign.should.be.exactly("test");
                data.msg.user.sex.should.be.exactly(1);
                done();
            });
    });

    it("addStar should work ok",function (done){
        request
            .post(host + "/api/token/userinfo/addfollow")
            .set("LoginToken",LoginToken)
            .send({
                starID:userID
            })
            .end(function(err,res){
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(err);
                }
                data.status.should.be.exactly(true);
                data.msg.starID.should.be.exactly(1);
                request
                    .post(host + "/api/token/userinfo")
                    .set("LoginToken", LoginToken)
                    .send({AuthorID: userID})
                    .end(function (err, res) {
                        try {
                            var data = JSON.parse(res.text);
                        } catch (e) {
                            console.log(err);
                        }
                        data.status.should.be.exactly(true);
                        data.msg.collectedStoriesCount.should.be.exactly(100);
                        data.msg.following.length.should.be.exactly(10);
                        done();
                    });
            });
    });
    
    it("delStar should work ok",function (done){
        request
            .post(host + "/api/token/userinfo/delfollow")
            .set("LoginToken",LoginToken)
            .send({
                starID:userID
            })
            .end(function(err,res){
                try {
                    var data = JSON.parse(res.text);
                } catch (e) {
                    console.log(err);
                }
                console.log(data.msg);
                data.status.should.be.exactly(true);
                request
                    .post(host + "/api/token/userinfo")
                    .set("LoginToken", LoginToken)
                    .send({AuthorID: userID})
                    .end(function (err, res) {
                        try {
                            var data = JSON.parse(res.text);
                        } catch (e) {
                            console.log(err);
                        }
                        data.status.should.be.exactly(true);
                        data.msg.collectedStoriesCount.should.be.exactly(100);
                        data.msg.following.length.should.be.exactly(9);
                        done();
                    });
            });
    });

    it("send a chat message should work ok",function(done){
        request
            .post(host + "/api/token/chat/send")
            .set("LoginToken",LoginToken)
            .send({
                revID:1,
                content:"this a test msg from 1 to 1"
            })
            .end(function(err,res){
                var data = JSON.parse(res.text);
                data.status.should.be.exactly(true);
                Msg.findOne({
                    where:{
                        sendID:1,
                        revID:1
                    }
                }).then(function(result){
                    result.content.should.be.exactly("this a test msg from 1 to 1");
                    done();
                });
            });
    });

    it("get all chat message of user 1 should work ok",function(done){
        request
            .post(host + "/api/token/chat/get")
            .set("LoginToken",LoginToken)
            .send({
                revID:1
            })
            .end(function(err,res){
                var data = JSON.parse(res.text);
                data.status.should.be.exactly(true);
                data.msg.msgs[0].content.should.be.exactly("this a test msg from 1 to 1");
                done();
            });
    });
});